local skill = fk.CreateSkill{
  name = "tea__zhuojun"
}
---@type TrigSkelSpec<TrigFunc>
local zhuojun = {
  anim_type = "offensive",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and target == player and data.card and player:getMark("tea__zhuojun_passed-phase") == 0 then
      local room = player.room
      local phase_event = room.logic:getCurrentEvent():findParent(GameEvent.Phase, true)
      local ids = {}
      if not phase_event then return end
      room.logic:getEventsByRule(GameEvent.UseCard, 1, function (e)
        local dat = e.data ---@type UseCardData
        if dat.from == player then
          local targets = dat.tos ---@type ServerPlayer[]
          room:sortByAction(targets)
          for _, id in ipairs(ids) do
            table.insertIfNeed(targets, id)
          end
          ids = targets or {}
        end
        return false
      end, phase_event.id)
      if #ids >= 2 then
        if ids[1] ~= ids[2] then
          room:setPlayerMark(player, "tea__zhuojun_passed-phase", 1)
          event:setCostData(self, {ids[1], ids[2]})
          return true
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local to1 = event:getCostData(self)[1]
    local to2 = event:getCostData(self)[2]
    return room:askToSkillInvoke(player, {
      skill_name = self.name,
      prompt = "#tea__zhuojun-invoke:"..to1.id..":"..to2.id
    })
  end,
  on_use = function(self, event, target, player, data)
    player:broadcastSkillInvoke(skill.name, table.random{1, 2})
    local room = player.room
    local to1 = event:getCostData(self)[1]
    local to2 = event:getCostData(self)[2]
    local array1, array2 = {to1}, {to1}
    local nextOne, lastOne = to1:getNextAlive(), to1:getLastAlive()
    while nextOne ~= to1 and #array1 < 100 do
      table.insert(array1, nextOne)
      if nextOne == to2 then break end
      nextOne = nextOne:getNextAlive()
    end
    while lastOne ~= to1 and #array2 < 100 do
      table.insert(array2, lastOne)
      if lastOne == to2 then break end
      lastOne = lastOne:getLastAlive()
    end
    local array = {}
    if #array1 ~= #array2 then
      array = #array1 < #array2 and array1 or array2
    elseif #array1 == 2 then
      array = array1
    else
      local choose = room:askToChoice(player, {
        choices = {"zhuojun_array1:"..to1.id, "zhuojun_array2:"..to1.id},
        skill_name = self.name,
        prompt = "#tea__zhuojun-choose",
      })
      array = choose == "zhuojun_array1:"..to1.id and array1 or array2
    end
    if #array > 0 then
      if table.contains(array, player) then
        table.removeOne(array, player)
      end
      local card = Fk:cloneCard("savage_assault")
      card.skillName = skill.name
      local use = { ---@type UseCardDataSpec
        from = player,
        tos = array,
        card = card,
      }
      room:useCard(use)
    end
  end,
}
---@type TrigSkelSpec<TrigFunc>
local zhuojun_refresh = {
  can_refresh = function(self, event, target, player, data)
    if not player:hasSkill(self, true, true) then return end
    if event == fk.DamageCaused then
      if data.card and target == player then
        return table.contains(data.card.skillNames, skill.name)
      end
    else
      if data.card and data.responseToEvent then
        if data.responseToEvent.card.name == "savage_assault" and table.contains(data.responseToEvent.card.skillNames, skill.name) then
          return data.card.name == "fire__slash" and not target.dead and not player.dead
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.DamageCaused then
      room:notifySkillInvoked(player, self.name, "offensive")
      player:broadcastSkillInvoke(skill.name, 3)
      data.damageType = fk.ThunderDamage
      return
    else
      room:notifySkillInvoked(player, self.name, "defensive")
      player:broadcastSkillInvoke(skill.name, 4)
      room:doIndicate(target, {player})
      room:damage{
        from = target,
        to = player,
        damage = 1, 
        skillName = self.name, 
        damageType = fk.FireDamage,
      }
    end
  end,
}
skill:addEffect(fk.CardUseFinished, zhuojun)
skill:addEffect(fk.DamageCaused, zhuojun_refresh)
skill:addEffect(fk.CardRespondFinished, zhuojun_refresh)
skill:addTest(function (room, me)
  local comp2 = room.players[2]
  local comp3 = room.players[3]
  FkTest.runInRoom(function ()
    room:handleAddLoseSkills(me, skill.name)
    GameEvent.Turn:create(TurnData:new(me, "game_rule", { Player.Play })):exec()
  end)
  FkTest.setNextReplies(me, { "1", "1", "1", "1" })
  FkTest.runInRoom(function ()
    room:useCard{
      from = me,
      tos = {me},
      card = Fk:cloneCard("ex_nihilo"),
    }
    room:useCard{
      from = me,
      tos = {comp2},
      card = Fk:cloneCard("slash"),
    }
  end)
end)
return skill